package vg.modules.search;
import java.util.HashMap;
import java.util.Map;
import java.util.Observable;
import java.util.Observer;
import java.util.Set;
import vg.core.IGraphView;
import vg.core.storableGraph.StorableSubGraph;
/**
* This class realizes model for search panel.
* It save all information of search plugin.
* @author tzolotuhin
*/
public class SearchPanelModel {
public static final int DEF_OPTION_MODE_1 = 1;
public static final int DEF_OPTION_MODE_2 = 2;
public static final int DEF_ACTION_MODE_1 = 1;
public static final int DEF_ACTION_MODE_2 = 2;
public static final int DEF_PAGE_1 = 1;
public static final int DEF_PAGE_2 = 2;
//-------------------------------------------
private int action;
private int option;
private boolean searchActive;
private boolean close;
private int pageNumber;
// variables for graph view
private Map<Integer, String>graphs;
private IGraphView workView;
private IGraphView currentView;
private final Observer workObserver; // for graph view (in real time selected different part of graph view)
private final Observer currentObserver;
//-------------------------------------------
private final FBSearchPanel fbSearchPanel;
/**
* Constructor.
* @param fbSearchPanel - feed back with search panel.
*/
public SearchPanelModel(final FBSearchPanel fbSearchPanel) {
this.fbSearchPanel = fbSearchPanel;
this.action = DEF_ACTION_MODE_1;
this.option = DEF_OPTION_MODE_1;
this.pageNumber = DEF_PAGE_1;
this.searchActive = false;
this.close = false;
this.graphs = new HashMap<Integer, String>(10);
this.workObserver = new Observer() {
public void update(Observable o, Object arg) {
if(SearchPanelModel.this.pageNumber == DEF_PAGE_1) {
if(SearchPanelModel.this.close == false) {
StorableSubGraph ssg = SearchPanelModel.this.workView.getSelectionSubGraph();
if(ssg == null) {
ssg = SearchPanelModel.this.workView.getStorableSubGraph();
}
Thread t = new Thread(new SetViewRunnable(SearchPanelModel.this.fbSearchPanel, ssg));
t.start();
}
}
}
};
this.currentObserver = new Observer() {
public void update(Observable o, Object arg) {
changeView(SearchPanelModel.this.currentView);
}
};
}
/**
* This method adds new graph to model.
* @param id - id of graph in data base.
* @param name - name of graph.
*/
public synchronized void addGraph(int id, String name) {
this.graphs.put(id, name);
}
public synchronized Map<Integer, String>getGraphs() {
return(this.graphs);
}
/**
* This method returns vertex additional attributes,
* which will show in new subgraph.
*/
public synchronized Set<String> getVertexAdditionalAttributes() {
return(this.fbSearchPanel.getVertexAdditionalAttributes());
}
public synchronized int getActionMode() {
int copyAction = this.action;
return(copyAction);
}
public synchronized int getOptionMode() {
int copyOption = this.option;
return(copyOption);
}
public synchronized void setActionMode(final int action) {
this.action = action;
}
public synchronized void setOptionMode(final int option) {
this.option = option;
Thread t = new Thread(new Runnable() {
public void run() {
SearchPanelModel.this.fbSearchPanel.setOptionMode(option);
}
});
t.start();
}
public synchronized void setSearchActive(final boolean searchActive) {
this.searchActive = searchActive;
Thread t = new Thread(new Runnable() {
public void run() {
SearchPanelModel.this.fbSearchPanel.setSearchEnable(SearchPanelModel.this.searchActive);
}
});
t.start();
}
public synchronized boolean isSearchActive() {
if(this.searchActive)
return(true);
return(false);
}
public synchronized void changeView(final IGraphView igv) {
// set current view
if(this.currentView != null) {
this.currentView.deleteObserver(this.currentObserver);
}
this.currentView = igv;
if(this.currentView != null) {
this.currentView.addObserver(this.currentObserver);
}
// set work view
if(this.pageNumber == DEF_PAGE_1) {
if(this.workView != null) {
this.workView.deleteObserver(this.workObserver);
}
this.workView = this.currentView;
if(this.workView != null) {
this.workView.addObserver(this.workObserver);
}
if(this.close == false) {
StorableSubGraph ssg = null;
if(this.workView != null) {
ssg = this.workView.getSelectionSubGraph();
if(ssg == null) {
ssg = this.workView.getStorableSubGraph();
}
}
Thread t = new Thread(new SetViewRunnable(this.fbSearchPanel, ssg));
t.start();
}
}
}
public synchronized void updateWorkView() {
changeView(this.currentView);
}
public synchronized void setClose(final boolean close) {
this.close = close;
if(!close) {
// open search panel
this.action = DEF_ACTION_MODE_1;
this.option = DEF_OPTION_MODE_1;
this.pageNumber = DEF_PAGE_1;
if(this.workView != null) {
this.workView.deleteObserver(this.workObserver);
}
this.workView = this.currentView;
if(this.workView != null) {
this.workView.addObserver(this.workObserver);
if(this.close == false) {
StorableSubGraph ssg = this.workView.getSelectionSubGraph();
if(ssg == null) {
ssg = this.workView.getStorableSubGraph();
}
Thread t = new Thread(new SetViewRunnable(this.fbSearchPanel, ssg));
t.start();
}
}
Thread t = new Thread(new Runnable() {
public void run() {
SearchPanelModel.this.fbSearchPanel.openFromModel();
}
});
t.start();
} else {
// close search panel
Thread t = new Thread(new Runnable() {
public void run() {
SearchPanelModel.this.fbSearchPanel.closeFromModel();
}
});
t.start();
}
}
public synchronized void search() {
if(this.searchActive) {
this.pageNumber = DEF_PAGE_2;
Thread t = new Thread(new Runnable() {
public void run() {
SearchPanelModel.this.fbSearchPanel.search();
}
});
t.start();
}
}
public synchronized void back() {
this.pageNumber = DEF_PAGE_1;
Thread t = new Thread(new Runnable() {
public void run() {
SearchPanelModel.this.fbSearchPanel.back();
}
});
t.start();
}
///////////////////////////////////////////////////////////////////////////
// OTHER PRIVATE CLASSES
///////////////////////////////////////////////////////////////////////////
/**
* This class realizes runnable interface for
* setting new storable sub graph to search panel.
* @author tzolotuhin
*/
private class SetViewRunnable implements Runnable {
private final StorableSubGraph ssg;
private final FBSearchPanel fbSearchPanel;
public SetViewRunnable(final FBSearchPanel fbSearchPanel, final StorableSubGraph ssg) {
this.ssg = ssg;
this.fbSearchPanel = fbSearchPanel;
}
public void run() {
this.fbSearchPanel.setCurrentSubGraph(this.ssg);
}
}
}